import pygame
import object
import defs

class Player(object.Object):
    def __init__(self, position_x, position_y):
        super(Player, self).__init__(position_x, position_y)

    def __init__(self, position_x, position_y, speed_y):
        super(Player, self).__init__(position_x, position_y, 0, speed_y)		

    def __init__(self, position_x, position_y, speed_rate, width, height):
        super(Player, self).__init__(position_x, position_y, 0, 0, width, height)
        self.ai_speed = defs.ai_speed
        self.speed_rate = speed_rate

    def updatePosition(self):
        object.Object.updatePosition(self)
        self.position_y = object.Object.inRangeY(self)		

    def setSpeed(self, speed_y):
        object.Object.setSpeed(self, 0, speed_y)

    def getRect(self):
        return pygame.rect.Rect(object.Object.getRect(self))

    def update(self, ball_position_y, ball_speed_x):
        # "AI"        
        if ball_speed_x < 0:
            # Try to catch the ball
            if ball_position_y < self.position_y:
                self.setSpeed(-self.ai_speed)
            elif ball_position_y > self.position_y + self.height:
                self.setSpeed(self.ai_speed)
        else:
            # Return to the center of the screen
            rect = self.getLimits()
            if ball_position_y < rect[1] + rect[3]/ 2:
                self.setSpeed(-self.ai_speed)
            elif ball_position_y > self.position_y + self.height:
                self.setSpeed(self.ai_speed)
